﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace Linkmeme
{
    public class GlobalApplication : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            CreateDefaultUserIfNotExists();
        }
        
        public static void RegisterRoutes(RouteCollection routes)
        {
            // Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
            //       automatic support on IIS6 and IIS7 classic mode

            routes.Add(new Route("Source/View/{sourceType}/{providerId}/{providerUserName}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Source", action = "View", sourceType = (string)null, providerId = (int?)null, providerUserName = (string)null }),
            });

            routes.Add(new Route("Source/ViewAll/{sourceType}/{groupby}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Source", action = "ViewAll", sourceType = (string)null, groupBy = "Provider" }),
            });



            //routes.Add(new Route("Photo/GroupList/{grouptype}", new MvcRouteHandler())
            //{
            //    Defaults = new RouteValueDictionary(new { Controller="Photo", action = "GroupList", id = "" }),
            //});

            routes.Add(new Route("Photo/ViewAll/{groupby}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Photo", action = "ViewAll", groupBy = "Provider" }),
            });

            routes.Add(new Route("Photo/View/{providerId}/{providerUserName}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Photo", action = "View", providerUserName = (string)null }),
            });

            routes.Add(new Route("Blog/View/{providerId}/{providerUserName}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Blog", action = "View", providerUserName = (string)null }),
            });

            routes.Add(new Route("Blog/ViewAll/{groupby}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Blog", action = "ViewAll", groupBy = "Provider" }),
            });

            routes.Add(new Route("Bookmark/View/{providerId}/{providerUserName}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Bookmark", action = "View", providerUserName = (string)null }),
            });

            routes.Add(new Route("Bookmark/ViewAll/{groupby}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "Bookmark", action = "ViewAll", groupBy = "Provider" }),
            });



            routes.Add(new Route("User/CheckUser/{providerid}/{username}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "User", action = "CheckUser", providerid = (int?)null, username=(string)null }),
            });

            routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),
            });

            routes.Add(new Route("Default.aspx", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
            });
        }


        private static void CreateDefaultUserIfNotExists()
        {
            const string DefaultUser = "admin";
            const string DefaultPassword = "admin";
            const string DefaultEmail = "admin@linkmeme.com";

            if (Membership.GetUser(DefaultUser) != null) return;

            Membership.CreateUser(DefaultUser, DefaultPassword, DefaultEmail);
        }
    }
}